package cn.zbmao.web.controller.user;

import cn.zbmao.web.bean.Constants;
import cn.zbmao.web.bean.User;
import cn.zbmao.web.interceptor.user.UserIntercepter;
import cn.zbmao.web.util.dao.DaoFactory;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.JsonRender;

import java.util.List;

public class UserDistrictController extends Controller {


	public void index(){
		User user = (User)getSessionAttr("user");
		List<Record>list = DaoFactory.getUserDao().getUser_district(user.getInt("id"));
		List<Record>url_list = DaoFactory.getUserDao().getUser_district_un(user.getInt("id"));
		setAttr("user_distruct_list", list);
		setAttr("district_list", url_list);
		render("/view/user_district.html");
	}

    /**
     * 地域添加与修改
     */
	@Before(UserIntercepter.class)
	public void add(){
        User user = (User)getSessionAttr("user");
        int key=getParaToInt("key", 0);
        String key1=getPara("key1",null);

        String[]key0=null;
        List<Record>list = DaoFactory.getUserDao().getUser_district(user.getInt("id"));
//       当key为-1时则为删除关注的地域，否则为添加地域
        if(key==-1){
            DaoFactory.getUserDao().deleteUser_districtByUserId(user.getInt("id"));
            if(key1!=null) {
                key0 = key1.split(",");
                for (int i = 0; i < key0.length; i++) {
                    Record level = DaoFactory.getUserDao().getUser_level(Integer.parseInt(key0[i]));
                    DaoFactory.getUserDao().addUser_district(Integer.parseInt(key0[i]), user.getInt("id"), Integer.parseInt(level.get("parentid") + ""));
                    setAttr("msg", 1);
                }

            }
            setAttr("msg", 1);
        }else {
            if (key1 != null) {
                key0 = key1.split(",");
//                判断关注的地域最多不超过5个
                if (list.size() + key0.length > 5) {
                    setAttr("listSize",list.size());
                    setAttr("msg", 2);

                } else {
//                    判断地域是否已经关注
                    for (int i = 0; i < key0.length; i++) {
                        if (DaoFactory.getUserDao().findUser_district(Integer.parseInt(key0[i]), user.getInt("id"))) {
                            setAttr("msg", 0);
                        } else {
                            Record level = DaoFactory.getUserDao().getUser_level(Integer.parseInt(key0[i]));
                            DaoFactory.getUserDao().addUser_district(Integer.parseInt(key0[i]), user.getInt("id"), Integer.parseInt(level.get("parentid") + ""));
                            setAttr("msg", 1);
                        }
                    }

                }
            } else if (key != 0 && key0 == null) {

                if (list.size() > 4) {
                    setAttr("listSize",list.size());
                    setAttr("msg", 2);
                } else {
                    if (DaoFactory.getUserDao().findUser_district(key, user.getInt("id"))) {
                        setAttr("msg", 0);
                    } else {
                        Record level = DaoFactory.getUserDao().getUser_level(key);
                        DaoFactory.getUserDao().addUser_district(key, user.getInt("id"), Integer.parseInt(level.get("parentid") + ""));
                        setAttr("msg", 1);
                    }
                }
            } else {
                setAttr("msg", -1);
            }
        }

		render(new JsonRender().forIE());
	}

    /**
     * 删除地域
     */
	@Before(UserIntercepter.class)
	public void delete(){
		DaoFactory.getUserDao().deleteUser_district(getParaToInt());
		redirect("/news/condition");
	}

    /**
     * 地域显示时为地域添加该地区对应的省份
     */
    @Before(UserIntercepter.class)
    public void findUserdis(){
        User user = (User)getSessionAttr("user");
        List<Record>list = DaoFactory.getUserDao().getUser_district(user.getInt("id"));
        for (Record record:list){
            if(record.getInt("loc_id")== Constants.LOC_ID_CITY){
                Record r=DaoFactory.getUserDao().getPidById(record.getInt("district_id"));
                record.set("name_d",r.getStr("name")+"  "+record.getStr("name"));
            }else{
                record.set("name_d",record.getStr("name"));
            }

        }
        setAttr("list",list);
        render(new JsonRender().forIE());
    }

    /**
     * 通过上级Id查询地域
     */
    @Before(UserIntercepter.class)
    public void findDis(){
        int code=getParaToInt("location_id");
        Record r=DaoFactory.getUserDao().getDisByCode(code);
        List<Record>list=DaoFactory.getLocationDao().getLocationList1(r.getInt("id"));
        setAttr("list",list);
        render(new JsonRender().forIE());
    }


}
